package com.whl.sm.controller.assets;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.dto.SelectDto;
import com.whl.sm.dto.TransferDto;
import com.whl.sm.model.assets.ComBuilding;
import com.whl.sm.service.assets.ComBuildingService;
import com.whl.sm.service.system.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-09
 */
@RestController
@RequestMapping("/com-building")
public class ComBuildingController extends BaseController {

    @Autowired
    private ComBuildingService comBuildingService;

    @Autowired
    private CommunityService communityService;

    @RequestMapping("/list")
    public Result getComList(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("bud_name");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> buildings = comBuildingService.page(getPage(),queryWrapper);
        buildings.getRecords().stream().forEach(obj->{
            ComBuilding comBuilding = (ComBuilding) obj;
            comBuilding.setComName(communityService.getById(comBuilding.getComId()).getComName());
        });
        return Result.ok(buildings);
    }


    @RequestMapping("/info/{id}")
    public Result buildingInfo(@PathVariable("id") String id){
        ComBuilding building = comBuildingService.getById(id);
        return Result.ok(building);
    }


    @RequestMapping("/update")
    public Result updateUser(@RequestBody ComBuilding building){
        comBuildingService.update(building);
        return Result.ok();
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            comBuildingService.update(new UpdateWrapper<ComBuilding>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }


    @RequestMapping("/selBudList/{comId}")
    public Result selectBuildings(@PathVariable("comId") String comId){
        List<ComBuilding> buildings = comBuildingService.list(new QueryWrapper<ComBuilding>().eq("com_id",comId));
        List<SelectDto> selBuds = buildings.stream().map(obj->{
            SelectDto option = new SelectDto();
            option.setValue(obj.getId());
            option.setLabel(obj.getBudName());
            option.setAlias(obj.getBudCode());
            return option;
        }).collect(Collectors.toList());
        return Result.ok(selBuds);
    }
}
